package com.system.user.socket;

import com.system.user.config.SpringUtils;
import com.system.user.dao.*;
import com.system.user.entity.UsageEntity;
import com.system.user.entity.UserEntity;
import com.system.user.entity.WaterEntity;
import com.system.user.weixin.WxPushServiceQcl;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.List;

/**
 * @author PGQing
 * @Desc 读取客户端数据的线程
 */
public class ReadClientDataThread implements Runnable {
	
	private InputStream is = null;

	Socket socket=null;

	@Autowired
	private UsageDao usageDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private GasDao gasDao;

	@Autowired
	private WaterDao waterDao;

	@Autowired
	private LightingDao lightingDao;

	@Autowired
	private ThresholdDao thresholdDao;

	public ReadClientDataThread(Socket socket) {
		try {
			is = socket.getInputStream();
			this.socket=socket;
		} catch (IOException e) {
			System.out.println("获取已连接会话中的输入流出现了错误：\n");
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		try {
			int lenght = 0;
			byte[] bytes = new byte[1024];
			String temp = null;
			while ((lenght = is.read(bytes)) != -1) {
				temp = new String(bytes, 0, lenght);
				System.out.println("接收："+temp);
				System.out.println(temp.length());
				if (temp.length() > 4 && temp.length() < 35) {
                    if (temp.contains("alarm")) {
                        System.out.println("检测到漏气-------" + temp);
                        // 漏气检测
                        // 推送小程序消息
                        sendMsg("漏气警报", "g1");
                    } else if (temp.contains("w") && temp.contains("E")) {
                        String watch = temp.substring(temp.indexOf("w") + 2, temp.indexOf("E"));
                        String light = temp.substring(temp.indexOf("E") + 2, temp.length() - 1);
                        System.out.println("水量：" + watch + "--------电量：" + light);
                        Double water = Double.parseDouble(watch);
                        Double lighting = Double.parseDouble(light) / 1000;
                        // 水电表插入，水量要除以1000转换成吨
                        usageDao = SpringUtils.getBean(UsageDao.class);
                        usageDao.insertUsage("w1", water);
                        usageDao.insertUsage("l1", lighting);
                        thresholdDao = SpringUtils.getBean(ThresholdDao.class);
                        Double wMax = thresholdDao.selectMax("w1");
                        Double lMax = thresholdDao.selectMax("l1");
                        if (wMax != null && water > wMax) {
                            sendMsg("用水量超量", "w1");
                        }
                        if (lMax != null && lighting > lMax) {
                            sendMsg("用电量超量", "l1");
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void sendMsg(String msg, String bNumber) {
        userDao = SpringUtils.getBean(UserDao.class);
        List<String> openid = userDao.selectOpenIDByBNumber(bNumber);
        String site = null;
        if (bNumber.contains("g")) {
            gasDao = SpringUtils.getBean(GasDao.class);
            site = gasDao.selectSite(bNumber);
        } else if (bNumber.contains("w")) {
            waterDao = SpringUtils.getBean(WaterDao.class);
            site = waterDao.selectSiteByNumber(bNumber);
        } else if (bNumber.contains("l")) {
            lightingDao = SpringUtils.getBean(LightingDao.class);
            site = lightingDao.selectSiteByNumber(bNumber);
        }
        WxPushServiceQcl wx = new WxPushServiceQcl();
        for (String o : openid) {
            String result = wx.pushAlert(o, msg, site);
            System.out.println(result);
        }
    }

}
